//
//  AppDelegate.swift
//  SCA
//
//  Created by tjun on 2025/8/31.
//

import UIKit
import IQKeyboardManagerSwift


@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        LanguageManager.shared.setLanguage()
        keyboardSetting()
        doInit()
        registerNotification()
        test()
        return true
    }
    
    private func doInit() {
        window = UIWindow(frame: UIScreen.main.bounds)
        guard let window = window else { return }
        window.backgroundColor = .white
        window.makeKeyAndVisible()
        ThemeManager.shared.setStyle()
        goToMainController()
    }
    
    private func goToMainController() {
        if UserInfo.isLogin {
            window!.rootViewController = MainController()
            loadInitData()
        } else {
            window!.rootViewController = NavigationController(rootViewController: LoginController())
        }
    }
    
    
    private func loadInitData() {
        MineService.loadUserData()
        WalletService.loadWalletInfo()        
    }
    
    func registerNotification () {
        NotificationCenter.default.addObserver(self, selector: #selector(handleUserLoginSuccessNotification), name: NotificationName.userLoginSuccess, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleUserLoginSuccessNotification), name: NotificationName.userRegisterSuccess, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleUserLogoutSuccessNotification), name: NotificationName.userLogout, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleTokenErrorNotification), name: NotificationName.tokenError, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleLanguageDidChangeNotification), name: NotificationName.languageDidChange, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(handleThemeDidDidChangeNotification), name: NotificationName.themeDidChange, object: nil)

    }
    
    @objc func handleThemeDidDidChangeNotification() {
//        goToMainController()
    }
    
    @objc func handleTokenErrorNotification() {
        guard UserInfo.isLogin else {
            return
        }
        
        UserInfo.isLogin = false
        window!.rootViewController = NavigationController(rootViewController: LoginController())
        UserInfo.removeToken()
        UserInfo.clear()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
            guard let controller = topViewController() else {
                return
            }
            IconMessageAlert(messsage: "登录已超时，请重新登录".localizedString, iconType: .succeed).showIn(controller)
        })
    }
    
    @objc func handleUserLoginSuccessNotification() {
        goToMainController()
    }
    
    @objc func handleLanguageDidChangeNotification(not: Notification) {
        guard let language = not.object as? AppLanguage else {
            return
        }
        printf("handleLanguageChangeNotification", language)
        goToMainController()
    }
    
    private func logout() {
        UserInfo.isLogin = false
        window!.rootViewController = NavigationController(rootViewController: LoginController())
        UserInfo.removeToken()
        UserInfo.clear()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
            guard let controller = topViewController() else {
                return
            }
            IconMessageAlert(messsage: "您已登出".localizedString, iconType: .succeed).showIn(controller)
        })
    }
    
    @objc func handleUserLogoutSuccessNotification() {
        logout()
    }
    
    
    func keyboardSetting () {
        let keyboardManager = IQKeyboardManager.shared
        keyboardManager.enable = true
        keyboardManager.enableAutoToolbar = false
        keyboardManager.shouldResignOnTouchOutside = true
        keyboardManager.keyboardDistanceFromTextField = -44
    }
    
    
    func test() {
        
//        NetworkManager.shared.post(url: "/power/personal/overview")
//        NetworkManager.shared.post(url: "/power/world/overview")

     
//        NetworkManager.shared.post(url: "/user/info")
//        NetworkManager.shared.post(url: "/user/wallet/info")
//        NetworkManager.shared.post(url: "/power/world/overview")
//        
//        
//        NetworkManager.shared.post(url: "/power/personal/overview")
//        
       
//
//        


//        NetworkManager.shared.post(url: "/user/wallet/mnemonic/verify", parameters: [
//            "step": "confirm",
//            "mnemonic": "twtwt"
//        ])


        
//                NetworkManager.shared.post(url: "/network/sponsor", parameters: [
//                     "type": "h"
//                 ])
        
        
//        NetworkManager.shared.post(url: "network/map")
//        NetworkManager.shared.post(url: "/wallet/list/sca")
//        NetworkManager.shared.post(url: "/wallet/deposit")

       
        
    }
}


 
